package org.example.leetCode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;

/**
 * @author Administrator
 */
public class QuickSort {
    public static void main(String[] args) {
        int[] nums = {4, 61, 13, 4, 984, 6, 1, 98, 74, 6};
        quickSort(nums, 0, nums.length - 1);
        for (int i : nums) {
            System.out.println(i);
        }

        HashSet set = new HashSet();

        int[] nums2 = {4, 61, 13, 4, 984, 6, 1, 98, 74, 6};
        Quick_Sort(nums2, 0, nums.length - 1);
        for (int i : nums2) {
            System.out.println(i);
        }
    }

    public static void quickSort(int[] nums, int left, int right) {
        if (left > right) {
            return;
        }

        int temp = nums[left];
        int i = left;
        int j = right;
        while (i != j) {
            while (nums[j] >= temp && i < j) {
                j--;
            }
            while (nums[i] <= temp && i < j) {
                i++;
            }
            if (i < j) {
                int exc = nums[i];
                nums[i] = nums[j];
                nums[j] = exc;
            }
        }
        nums[left] = nums[i];
        nums[i] = temp;
        quickSort(nums, left, i - 1);
        quickSort(nums, i + 1, right);
    }

    static void Quick_Sort(int[] arr, int begin, int end) {
        if (begin >= end) {
            return;
        }
        int temp = arr[begin];
        int m = begin + 1;
        int n = end;

        while (m < n) {
            while (temp > arr[m] && m < n) {
                m++;
            }
            while (temp < arr[n] && m < n) {
                n--;
            }
            int exc = arr[m];
            arr[m] = arr[n];
            arr[n] = exc;
        }
        arr[begin] = arr[m];
        arr[m] = temp;
        Quick_Sort(arr, begin, m - 1);
        Quick_Sort(arr, m + 1, end);
    }
}
